using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.IO;

public class lwBuildVariable
{
	public lwBuildVariable(string name, string val, /*string type,*/ string enumValues, bool bBuilderVisibility)	
	{
		m_name = name;
		m_value = val;
		//m_type = type;
		m_enumValues = enumValues;
		m_enumValue = -1;
		m_bBuilderVisibility = bBuilderVisibility;
	}

	public string m_name;
	public string m_value;
	//public string m_type;
	public string m_enumValues;
	public int m_enumValue;
	public bool m_bBuilderVisibility = false;
}

public class lwBuildSettings 
{
	private static Dictionary<string,lwBuildVariable> m_variables = null;
	
	public static Dictionary<string,lwBuildVariable> Variables
	{
		get 
		{
			if (m_variables == null)
			{
				m_variables = new Dictionary<string,lwBuildVariable>();
				Load();
			}
			return m_variables;
		}
	}
	
	public static void ReadXml( ref XmlReader xr )
	{
		if ( xr != null )
		{
	        while (xr.Read())
	        {
	            if (xr.NodeType == XmlNodeType.Element )
				{
					switch( xr.Name.ToUpper() )
					{
					case "VARIABLE":
						{
							string sVarName = xr.GetAttribute( "name");
							string sVarValue = xr.GetAttribute("value");
							string sEnumValues =  xr.GetAttribute("enumValues");
							if ( !string.IsNullOrEmpty(sEnumValues) )
								sEnumValues = sEnumValues.Replace(" ","");
							bool bBuilderVisibility = lwTools.GetXmlAttributeBool( xr, "builder", true);
							lwBuildVariable newVar = new lwBuildVariable(sVarName, sVarValue, sEnumValues, bBuilderVisibility); 
							if ( m_variables.ContainsKey( sVarName ) )
								m_variables.Remove( sVarName );
							m_variables.Add(sVarName, newVar);
						}
						break;
					}
				}
			}
			xr.Close();
		}
	}
	
	public static void LoadInitSettings( string sIniFile )
	{
#if !UNITY_IPHONE
		if ( Variables != null ) // Ce test déclenche le chargement du Settings.enc (voir Property Variables)
		{
			if ( !string.IsNullOrEmpty( sIniFile ) )
			{
				XmlReader xr = XmlReader.Create(new StringReader(sIniFile));
				ReadXml( ref xr );
			}
		}
#endif
	}
	
	public static void Load(  )
	{
		Debug.Log("lwBuildSettings.Load");
		Variables.Clear();
#if UNITY_EDITOR	
		string sFilePath =  Application.dataPath + "/Resources/BuildSettings.txt";
		XmlReader xr = XmlReader.Create( sFilePath );
#else
		TextAsset textAsset = Resources.Load( "BuildSettings" ) as TextAsset;
		string sText = textAsset.text;
		Debug.Log("text = " + sText);
		StringReader stringReader = new StringReader(sText);
		stringReader.Read();	//skip bom
		Debug.Log("stringreader built");
		XmlReader xr = XmlReader.Create( stringReader );	
#endif	
		ReadXml( ref xr );
	}

#if UNITY_EDITOR	
	public static void Save()
	{
		string sFilePath =  Application.dataPath + "/Resources/BuildSettings.txt";
		XmlWriterSettings settings = new XmlWriterSettings();
		settings.Indent = true;
		settings.IndentChars = ( "\t" );
		settings.Encoding = System.Text.Encoding.UTF8;
		settings.NewLineOnAttributes = false;

		XmlWriter xw = XmlWriter.Create( sFilePath, settings );
		xw.WriteStartElement("SETTINGS");
		foreach (KeyValuePair<string,lwBuildVariable> v in Variables)
		{
			lwBuildVariable buildVar = v.Value as lwBuildVariable;

			xw.WriteStartElement("VARIABLE");
			xw.WriteAttributeString("name", v.Key);
			xw.WriteAttributeString("value", buildVar.m_value);
			xw.WriteAttributeString("builder", buildVar.m_bBuilderVisibility?"true":"false") ;
			if ( !string.IsNullOrEmpty( buildVar.m_enumValues ) )
				xw.WriteAttributeString("enumValues", buildVar.m_enumValues);

			xw.WriteEndElement();
		}
		xw.WriteEndElement();
		xw.Flush();
		xw.Close();
	}
#endif
	
	public static string GetEnumVariableValues(string variableName)
	{
		lwBuildVariable buildVar = null;
		Variables.TryGetValue( variableName, out buildVar );
		return buildVar != null ? buildVar.m_enumValues : "";
	}

	public static string GetVariableValue(string variableName)
	{
		lwBuildVariable buildVar = null;
		Variables.TryGetValue( variableName, out buildVar );
		return buildVar != null ? buildVar.m_value : "";
	}

	public static void SetVariableValue(string variableName, string sValue)
	{
		lwBuildVariable buildVar = Variables[variableName];
		buildVar.m_value = sValue;
		buildVar.m_enumValue = -1;
	}

	public static bool GetBoolVariableValue(string variableName)
	{
		return lwTools.ReadBool(GetVariableValue(variableName),false);
	}

	public static void SetBoolVariableValue(string variableName, bool bValue)
	{
		SetVariableValue(variableName, bValue ? "true" : "false");
	}

	public static int GetEnumVariableValue(string variableName)
	{
		lwBuildVariable buildVar = null;
		Variables.TryGetValue( variableName, out buildVar );
		if (buildVar != null)
		{
			if (buildVar.m_enumValue == -1)
			{
				string [] values = buildVar.m_enumValues.Split(',');
				for (int i=0; i<values.Length; i++)
				{
					if (values[i] == buildVar.m_value)
					{
						buildVar.m_enumValue = i;
						return i;
					}
				}
				Debug.LogError("Invalid enum value: " + buildVar.m_value);
			}
			return buildVar.m_enumValue;
		}
		return -1;
	}
	
	public static bool GetXmlAttributeBool( XmlReader xr, string sAttribute, bool bDefault )
	{
		return lwTools.GetXmlAttributeBool( xr, sAttribute, bDefault );
	}

	// ----------------------------------------------------------------------------------
	// ----------------------------------------------------------------------------------
	// Fonctions static permettant d'accéder a certains variables communément utilisées
	// ----------------------------------------------------------------------------------
	// ----------------------------------------------------------------------------------
	static public int GetGameVersion()
	{
		return GetEnumVariableValue("GAME_VERSION");
	}
	static public string GetGameVersionName()
	{
		return GetVariableValue("GAME_VERSION");
	}
	
	static public bool IsBfgBuild()
	{
		return GetBoolVariableValue("BIGFISH");
	}
	static public bool IsFreemiumBuild()
	{
		return GetBoolVariableValue("FREEMIUM");
	}
	static public bool IsReleaseBuild()
	{
		return GetBoolVariableValue("RELEASE");
	}
	static public bool IsCheatEnabled()
	{
		return GetBoolVariableValue("CHEAT");
	}
	static public bool IsPartnerBuild()
	{
		return GetBoolVariableValue("PARTNER");
	}
	static public bool IsSurveyBuild()
	{
		return GetBoolVariableValue("SURVEY");
	}
	static public bool IsExoticBuild()
	{
		return GetBoolVariableValue("EXOTIC");
	}
	static public string GetSpriteRootPath()
	{
		return GetVariableValue("SPRITE_ROOT");
	}
	static public string GetImagesFolder()
	{
		return Application.dataPath + GetVariableValue("SPRITE_ROOT");
	}
}
